package tongwentongshu.com.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tencent.imsdk.TIMFriendshipManager;
import com.tencent.imsdk.TIMUserProfile;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.qcloud.ui.CircleImageView;

import java.util.List;

import tongwentongshu.com.app.R;
import tongwentongshu.com.app.modelim.Message;


/**
 * 聊天界面adapter
 */
public class ChatAdapter extends ArrayAdapter<Message> {

    private final String TAG = "ChatAdapter";

    private int resourceId;
    private View view;
    private ViewHolder viewHolder;
    private String  imageUrl;
    private Context context;
    private String userImage;

    /**
     * Constructor
     *
     * @param context  The current context.
     * @param resource The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     * @param objects  The objects to represent in the ListView.
     */
    public ChatAdapter(Context context, int resource, List<Message> objects , String imageUrl) {
        super(context, resource, objects);
        this.context = context ;
        resourceId = resource;
        this.imageUrl = imageUrl ;

        //获取自己的资料
        TIMFriendshipManager.getInstance().getSelfProfile(new TIMValueCallBack<TIMUserProfile>(){
            @Override
            public void onError(int code, String desc){
                //错误码code和错误描述desc，可用于定位请求失败原因
                //错误码code列表请参见错误码表
            }

            @Override
            public void onSuccess(TIMUserProfile result){

                userImage = result.getFaceUrl();
                Log.e("shiqiangMy", "identifier: " + result.getIdentifier() + " nickName: " + result.getNickName()
                        + " getFaceUrl: " + result.getFaceUrl() + " allow: " + result.getAllowType());
            }
        });



    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView != null){
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }else{
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.leftMessage = (RelativeLayout) view.findViewById(R.id.leftMessage);
            viewHolder.rightMessage = (RelativeLayout) view.findViewById(R.id.rightMessage);
            viewHolder.leftPanel = (RelativeLayout) view.findViewById(R.id.leftPanel);
            viewHolder.rightPanel = (RelativeLayout) view.findViewById(R.id.rightPanel);
            viewHolder.sending = (ProgressBar) view.findViewById(R.id.sending);
            viewHolder.error = (ImageView) view.findViewById(R.id.sendError);
            viewHolder.sender = (TextView) view.findViewById(R.id.sender);
            viewHolder.rightDesc = (TextView) view.findViewById(R.id.rightDesc);
            viewHolder.systemMessage = (TextView) view.findViewById(R.id.systemMessage);
            //左边还有的头像
            viewHolder.leftAvatar = (CircleImageView) view.findViewById(R.id.leftAvatar);
            viewHolder.rightAvatar = (CircleImageView) view.findViewById(R.id.rightAvatar);
            view.setTag(viewHolder);
        }
        if (position < getCount()){
            final Message data = getItem(position);
            data.showMessage(viewHolder, getContext());
        }

        if (!TextUtils.isEmpty(imageUrl)){
            Glide.with(context).load(imageUrl).into(viewHolder.leftAvatar);
        }else{
            viewHolder.leftAvatar.setImageDrawable(context.getResources().getDrawable(R.drawable.head_img));
        }
        if (!TextUtils.isEmpty(userImage)){
            Glide.with(context).load(userImage).into(viewHolder.rightAvatar);
        }else{
            viewHolder.rightAvatar.setImageDrawable(context.getResources().getDrawable(R.drawable.head_img));
        }

        return view;
    }


    public class ViewHolder{
        public RelativeLayout leftMessage;
        public RelativeLayout rightMessage;
        public RelativeLayout leftPanel;
        public RelativeLayout rightPanel;
        public ProgressBar sending;
        public ImageView error;
        //左边好友的头像设置
        public CircleImageView leftAvatar;
        //自己的头像
        public CircleImageView rightAvatar;
        public TextView sender;
        public TextView systemMessage;
        public TextView rightDesc;
    }
}
